<template>
  <div v-drag>
    <div class="mask" @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>{{ channelId ? '修改' : '新增' }}消息渠道</p>
        <i @click="closePopup(false)"></i>
      </div>
      <div class="box_body">
        <div class="line">
          <span class="lab"><i class="mark_icon">*</i>渠道名称：</span>
          <div :class="{'error_border':tipNum===1}">
            <el-input type="text" placeholder="请输入渠道名称" v-model.trim="channelName"
                      @input="setTip('','')"></el-input>
          </div>
        </div>

        <div class="line">
          <span class="lab"><i class="mark_icon">*</i>渠道配置：</span>
          <div :class="{'error_border':tipNum===2}">
            <el-input type="textarea" placeholder="请输入渠道配置" v-model.trim="config" :rows="6"
                      @input="setTip('','')"></el-input>
          </div>
        </div>
        <div class="line">
          <span class="lab"><i class="mark_icon">*</i>消息类型：</span>
          <div :class="{'error_border':tipNum===3}">
            <el-select
              v-model="noticeType"
              placeholder="请选择类型">
              <el-option
                v-for="(item,index) in typeOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="line">
          <span class="lab">状态：</span>
          <div>
            <el-radio-group v-model="proStatus">
              <el-radio :label="1">可用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </div>
        </div>
        <p class="error_line">{{ infoTip }}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirmEdit">确定</span>
      </div>
    </div>
  </div>
</template>

<script>


import {getMessageChannelDetail, getMessageChannelType,addMessageChannel,updateMessageChannel} from "../../../api/data/channel";

export default {
  props: ['channelId'],
  data() {
    return {
      config: '',
      channelName: '',
      noticeType: '',
      typeOption:[],
      proStatus: 1,
      infoTip: '',
      tipNum: '',
      loading: false,
    }
  },
  mounted() {
    this.getType();
    if (this.channelId){
      this.getDetail()
    }
  },
  methods: {
    // 获取消息类型
    getType() {
      getMessageChannelType().then(res => {
        this.typeOption = res.data ? res.data : [];
      });
    },

    getDetail() {
      getMessageChannelDetail( this.channelId).then(res => {
        if (res.data) {
          let info = res.data;
          this.config = info.config ? info.config : '';
          this.channelName = info.name ? info.name : '';
          this.noticeType = info.noticeType ? info.noticeType : '';
          this.proStatus = info.enable ? info.enable : 1;
        }
      });
    },

    // 确认编辑
    confirmEdit() {
      if (!this.channelName) {
        this.setTip(1, '请输入渠道名称！')
      } else if (this.config === '') {
        this.setTip(2, '请输入渠道配置！')
      } else if (this.noticeType === '') {
        this.setTip(3, '请选择渠道类型！')
      } else {
        this.sendInfo()
      }
    },

    sendInfo() {
      let that = this;
      that.loading = true;
      let data = {
        enable: that.proStatus,
        noticeType: that.noticeType,
        id: that.channelId,
        name: that.channelName,
        config: that.config
      }
      if (that.channelId) {
        updateMessageChannel(JSON.stringify(data)).then(response => {
          that.setResult(response.success);
        }).catch((e) => {
          that.loading = false;
        });
      } else {
        addMessageChannel(JSON.stringify(data)).then(response => {
          that.setResult(response.success);
        }).catch((e) => {
          that.loading = false;
        });
      }
    },

    setResult(finish){
      if (finish){
        this.closePopup(true);
        this.$message.success('操作成功！');
      }
      this.loading = false;
    },

    setTip(num, text) {
      this.tipNum = num;
      this.infoTip = text
    },

    // 关闭
    closePopup(refresh) {
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>
.box_body {
  min-height: 300px;
}

</style>
